package com.sgcc.fj.pmos.e_fj_home.activity;


import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.arouter.home.HomeArouterPath;
import com.sgcc.fj.pmos.common.ItemDecoration.GridItemDecoration;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.fj.pmos.common.utils.DateUtils;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_home.adapter.DDSNDLPHYDAdapter;
import com.sgcc.fj.pmos.e_fj_home.bean.DDSNDLPHYDBean;
import com.sgcc.fj.pmos.e_fj_home.contract.DDSNDLPHYDContract;
import com.sgcc.fj.pmos.e_fj_home.presenter.DDSNDLPHYDPresenter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 调度省内电力平衡裕度  或者   调度可再生能源富余程度
 * sn_xhpl_dlphyd_7_4        sn_xhpl_rqkzsfy_7_5
 */
@Route(path = HomeArouterPath.HOME_DDSNDLPHYD)
public class DDSNDLPHYDActivity extends MvpBaseRecycleActivity<DDSNDLPHYDBean.DataBean.IpConfigDataColsListBean, DDSNDLPHYDPresenter> implements DDSNDLPHYDContract.View {

    private String mLabel, mInfoCode, dateTime;

    @Override
    protected DDSNDLPHYDPresenter createPresenter() {
        return new DDSNDLPHYDPresenter();
    }

    @Override
    public void getDataSuccess(DDSNDLPHYDBean response) {
        if (response.getData().getIpConfigDataColsList() != null) {
            List<DDSNDLPHYDBean.DataBean.IpConfigDataColsListBean> list = response.getData().getIpConfigDataColsList();
            for (int i = 0; i < list.size(); i++) {
                if ("updateTime".equals(list.get(i).getColName())) {
                    list.remove(i);
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = response.getData().getObjectList().getList().get(0);
                Set<String> keySet = map.keySet();
                //遍历键的集合，得到每一个键
                for (String key : keySet) {
                    //由键找值
                    String value = map.get(key);
                    if (list.get(i).getColName().equals(key)) {
                        list.get(i).setColName(value);
                    }
                }
            }
            loadSuccess(false, list);
        }
    }

    @Override
    public void getDataFailed() {

    }

    @Override
    protected void setTitleBar(TitleBar title_bar) {
        title_bar.setTitle(mLabel);
        title_bar.tvRight.setTextSize(10);
        title_bar.setRightIconAndText(R.mipmap.down_jiantou, dateTime, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDateSelect();
            }
        });
    }

    @Override
    protected void setRecycleViewLayoutManager(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.addItemDecoration(new GridItemDecoration());
    }

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        mLabel = getIntent().getStringExtra("label");
        mInfoCode = getIntent().getStringExtra("infoCode");
        dateTime = DateUtils.getCurrentTimes1();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new DDSNDLPHYDAdapter(mContext);
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getData(listSize, mInfoCode, dateTime, isloading);
    }

    private void showDateSelect() {
        Calendar startDate = Calendar.getInstance();
        startDate.set(2021, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2099, 11, 31);

        Calendar selectedDate = Calendar.getInstance();
        try {
            String dateStr = title_bar.tvRight.getText().toString().trim();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(dateStr);
            selectedDate.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DialogUtil.showDateSelectDialog(mContext,
                new boolean[]{true, true, true, false, false, false},
                startDate, endDate, selectedDate, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String string = TimeUtils.date2String(date, "yyyy-MM-dd");
                        title_bar.tvRight.setText(string);
                        dateTime = string;
                        refresh(true);
                    }
                }
        );
    }
}
